﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LibraryO2S;
using System.IO;
using System.Text;

public partial class controls_admin_Support : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String hiddenFieldValue = Request["theHiddenTabIndex"] ?? "0"; ;
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSupport();
            if (Request["index"] != null)
                hiddenFieldValue = "1";
        }
        StringBuilder js = new StringBuilder();
        js.Append("<script type='text/javascript'>");
        js.Append("var previouslySelectedTab = ");
        js.Append(hiddenFieldValue);
        js.Append(";</script>");
        ltrScript.Text = js.ToString();
    }
    private DataSet createObjDS()
    {
        DataSet objDS = new DataSet("Supports");
        string path = Server.MapPath("../upload/xml/Support.xml");
        if (File.Exists(path))
        {
            objDS.ReadXml(path);
            if (objDS == null || objDS.Tables.Count <= 0)
                objDS.Tables.Add(createObjDT());
        }
        else
            objDS.Tables.Add(createObjDT());
        return objDS;
    }
    private DataTable createObjDT()
    {
        DataTable objDT = new DataTable("Support");
        objDT.Columns.Add("NAME", typeof(String));
        objDT.Columns.Add("YAHOO", typeof(String));
        objDT.Columns.Add("PHONE", typeof(String));
        return objDT;
    }
    private void loadSupport()
    {
        try
        {
            DataSet objDS = createObjDS();
            if (Request["index"] != null)
            {
                txtName.Text = objDS.Tables[0].Rows[Convert.ToInt32(Request["index"]) - 1]["NAME"].ToString();
                txtYahoo.Text = objDS.Tables[0].Rows[Convert.ToInt32(Request["index"]) - 1]["YAHOO"].ToString();
                //txtPhone.Text = objDS.Tables[0].Rows[Convert.ToInt32(Request["index"]) - 1]["PHONE"].ToString();
            }
            DataTable objDT = objDS.Tables[0];
            bindRepeater(rptSupport, objDT, divMessage);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi :<br />" + ex.Message);
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            string name = txtName.Text.Trim();
            string yahoo = txtYahoo.Text.Trim();
            string phone = "09";// txtPhone.Text.Trim();
            DataSet objDS = createObjDS();
            DataRow drAdv = objDS.Tables[0].NewRow();
            if (hdfAdd.Value.Equals("0") && Request["index"] != null)
                drAdv = objDS.Tables[0].Rows[Convert.ToInt32(Request["index"]) - 1];
            drAdv["NAME"] = name;
            drAdv["YAHOO"] = yahoo;
            drAdv["PHONE"] = phone;
            if (Request["index"] == null || hdfAdd.Value.Equals("1"))
                objDS.Tables[0].Rows.InsertAt(drAdv, 0);
            objDS.WriteXml(Server.MapPath("../upload/xml/Support.xml"));
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công.");
            txtName.Text = "";
            txtYahoo.Text = "";
            loadSupport();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi :<br />" + ex.Message);
        }
    }
    protected void rptAdvertise_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            DataSet objDS = createObjDS();
            objDS.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
            objDS.WriteXml(Server.MapPath("../upload/xml/Support.xml"));
            loadSupport();
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Đã xóa thành công.");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi xảy ra: " + ex.Message);
        }
    }
}
